﻿using PLCS.Application.Contracts.LogShowApp.Dtos;
using PLCS.Application.Contracts.SolutionApp.Dtos;
using PLCS.Client.IService.LogManager;

namespace PLCS.App.ProcessSolution.ViewModels.Solution;

public class SolutionDetailViewModel : HostDialogViewModel
{
    private SolutionDto _model;

    public SolutionDto Model
    {
        get { return _model; }
        set { SetProperty(ref _model, value); }
    }

    #region 步骤集合

    private ObservableCollection<SolutionStepDto> _solutionSteps;

    public ObservableCollection<SolutionStepDto> SolutionSteps
    {
        get { return _solutionSteps; }
        set { SetProperty(ref _solutionSteps, value); }
    }

    #endregion 步骤集合

    #region 选中的工序

    private string _selectProcessItem;

    public string SelectProcessItem
    {
        get { return _selectProcessItem; }
        set { SetProperty(ref _selectProcessItem, value); }
    }

    #endregion 选中的工序

    #region 可选工序集合

    private ObservableCollection<string> _canSelectProcessItems;

    public ObservableCollection<string> CanSelectProcessItems
    {
        get { return _canSelectProcessItems; }
        set { SetProperty(ref _canSelectProcessItems, value); }
    }

    #endregion 可选工序集合

    #region 选中的工序集合

    private string _selectProcesslist;

    public string SelectProcesslist
    {
        get { return _selectProcesslist; }
        set { SetProperty(ref _selectProcesslist, value); }
    }

    #endregion 选中的工序集合

    #region 步骤字段

    public Guid EditStepId { get; set; }

    private string _stepNo;

    public string StepNo
    {
        get { return _stepNo; }
        set { SetProperty(ref _stepNo, value); }
    }

    private string _stepOrder;

    public string StepOrder
    {
        get { return _stepOrder; }
        set { SetProperty(ref _stepOrder, value); }
    }

    #endregion 步骤字段

    #region 步骤编辑弹窗是否开启

    private bool _isEditStepDialogOpen;

    public bool IsEditStepDialogOpen
    {
        get { return _isEditStepDialogOpen; }
        set { SetProperty(ref _isEditStepDialogOpen, value); }
    }

    #endregion 步骤编辑弹窗是否开启

    #region 步骤编辑或新增标志位

    private string _editOrAddStepTitle;
    private readonly ISolutionService _solutionService;
    private readonly IProcessService _processService;
    private readonly IObjectMapper<ProcessSolutionModule> _objectMapper;

    public string EditOrAddStepTitle
    {
        get { return _editOrAddStepTitle; }
        set { SetProperty(ref _editOrAddStepTitle, value); }
    }

    #endregion 步骤编辑或新增标志位

    #region 步骤编辑或新增

    public DelegateCommand AddItemCommand => new DelegateCommand(AddOrEditItem);

    private void AddOrEditItem()
    {
        if (SolutionSteps == null)
            SolutionSteps = new ObservableCollection<SolutionStepDto>();
        string finalProcess = string.Empty;
        if (!SelectProcesslist.Contains(ConstantChar.SplitChar))
            finalProcess = SelectProcesslist;
        if (EditOrAddStepTitle == "新增")
        {
            SolutionSteps.Add(new SolutionStepDto()
            {
                StepNo = StepNo,
                StepOrder = StepOrder,
                Process = SelectProcesslist,
                FinalProcess = finalProcess
            });
        }
        else
        {
            var step = SolutionSteps.FirstOrDefault(x => x.Id == EditStepId);
            SolutionSteps.Remove(step);

            if (step != null)
            {
                step.StepNo = StepNo;
                step.StepOrder = StepOrder;
                step.Process = SelectProcesslist;
                step.FinalProcess = finalProcess;
            }
            SolutionSteps.Add(step);
        }

        IsEditStepDialogOpen = false;
    }

    #endregion 步骤编辑或新增

    #region 添加一个工序

    public DelegateCommand<string> AddProcessItemCommand => new DelegateCommand<string>(AddProcessItem);

    private void AddProcessItem(string process)
    {
        if (SelectProcesslist.IsNullOrEmpty())
        {
            SelectProcesslist += process;
        }
        else
        {
            SelectProcesslist += $"{ConstantChar.SplitChar}{process}";
        }
    }

    #endregion 添加一个工序

    #region 清除工序

    public DelegateCommand<string> ClearProcessItemCommand => new DelegateCommand<string>(ClearProcessItem);

    private void ClearProcessItem(string process)
    {
        if (!SelectProcesslist.IsNullOrEmpty())
            SelectProcesslist = string.Empty;
    }

    #endregion 清除工序

    #region 工序下拉框刷新事件

    public DelegateCommand DropOpenedCommand => new DelegateCommand(DropOpened);

    private async void DropOpened()
    {
        var res = await _processService.GetListAsync();
        CanSelectProcessItems = res.Select(x => x.ProcessNo).ToObservable();
    }

    #endregion 工序下拉框刷新事件

    #region 删除步骤

    public DelegateCommand<SolutionStepDto> DeleteItemCommand => new DelegateCommand<SolutionStepDto>(DeleteItem);

    private void DeleteItem(SolutionStepDto model)
    {
        SolutionSteps.Remove(model);
    }

    #endregion 删除步骤

    #region 编辑步骤

    public DelegateCommand<SolutionStepDto> EditStepCommand => new DelegateCommand<SolutionStepDto>(EditStep);

    private void EditStep(SolutionStepDto step)
    {
        IsEditStepDialogOpen = true;
        EditOrAddStepTitle = "修改";
        StepNo = step.StepNo;
        StepOrder = step.StepOrder;
        SelectProcesslist = step.Process;
        EditStepId = step.Id;
    }

    #endregion 编辑步骤

    #region 新增步骤

    public DelegateCommand AddStepCommand => new DelegateCommand(AddStep);

    private void AddStep()
    {
        IsEditStepDialogOpen = true;
        EditOrAddStepTitle = "新增";
        StepNo = "";
        StepOrder = "";
        SelectProcesslist = "";
        EditStepId = Guid.Empty;
    }

    #endregion 新增步骤

    public override void OnDialogOpened(IDialogParameters parameters)
    {
        if (parameters.ContainsKey("Value"))
        {
            Model = parameters.GetValue<SolutionDto>("Value");
            SolutionSteps = Model.SolutionSteps.ToObservable();
            Title = "编辑";
        }
        else
        {
            Title = "新增";
            Model = new SolutionDto();
        }
    }

    protected override async void Save()
    {
        try
        {
            SavebuttonIsEnabled = false;
            Model.SolutionSteps = SolutionSteps.ToList();
            var solution = _objectMapper.Map<SolutionDto, SolutionEditDto>(Model);
            if (solution.Id != Guid.Empty)
            {
                await _solutionService.UpdateAsync(solution);
                Log("修改", solution);
            }
            else
            {
                await _solutionService.CreateAsync(solution);
                Log("新增", solution);
            }

            ShowMessage("操作成功");
            base.Save();
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
        }
        finally { SavebuttonIsEnabled = true; }
    }

    public SolutionDetailViewModel(ISolutionService solutionService,
        IProcessService processService,
        IObjectMapper<ProcessSolutionModule> objectMapper)
    {
        _solutionService = solutionService;
        _processService = processService;
        _objectMapper = objectMapper;
    }
}